package com.yz.locationsimulation;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yz.locationsimulation.mapper.LocationMapper;
import com.yz.locationsimulation.model.Location;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@SpringBootApplication
@MapperScan("com.yz.locationsimulation.mapper")
public class LocationSimulationApplication {

    @Autowired
    private LocationMapper locationMapper;

    private Location baseLocation;

    public static void main(String[] args) {
        SpringApplication.run(LocationSimulationApplication.class, args);
    }

    @GetMapping("/index")
    public Object index() {
        Location location = new Location();
        location.setVehiclePlate("");
//        List<Location> locationList = locationMapper.selectList(new QueryWrapper<Location>().last("limit 5"));
        baseLocation.setLongitude(randomDouble(baseLocation.getLongitude()));
        baseLocation.setLatitude(randomDouble(baseLocation.getLatitude()));
        baseLocation.setLocationSpeed(randomSpreed());
        baseLocation.setLocationTime(LocalDateTime.now());
        locationMapper.insert(baseLocation);
        return baseLocation;
    }

    public int randomSpreed() {
        return RandomUtil.randomInt(100, 200);
    }

    public Double randomDouble(Double current) {
        double temp = RandomUtil.randomDouble(0.0001, 0.0009);
        temp = NumberUtil.round(temp, 6).doubleValue();
        int randomInt = RandomUtil.randomInt(0, 100);
        boolean isAdd = randomInt % 2 == 0;
        Double result;
        if (isAdd) {
            result = NumberUtil.add(current.doubleValue(), temp);
        } else {
            result = NumberUtil.sub(current.doubleValue(), temp);
        }
        Console.log("{} {}  {} = {}", current, isAdd ? "+" : "-", temp, result);
        return result;

    }

    private String str = "{\n" +
            "    \"vehiclePlate\": \"民航-C2969\",\n" +
            "    \"alarmSign\": 0,\n" +
            "    \"state\": 786435,\n" +
            "    \"longitude\": 113.30106,\n" +
            "    \"latitude\": 23.40398,\n" +
            "    \"altitude\": 3,\n" +
            "    \"locationSpeed\": 185,\n" +
            "    \"direction\": 109,\n" +
            "    \"locationTime\": \"2020-12-06T18:40:34\",\n" +
            "    \"mileage\": 37938,\n" +
            "    \"oil\": 0,\n" +
            "    \"pulseSpeed\": 0,\n" +
            "    \"in_fz\": 1\n" +
            "}";

    {
        baseLocation = JSONUtil.parseObj(str).toBean(Location.class);
    }
}
